$(function () {

    //操作时间
    $('#cTime_start').daterangepicker({
        "singleDatePicker": true,
        "timePicker": true,
        "timePicker24Hour": true,
        "timePickerSeconds": true,
        "parentEl": "200",
        locale: {
            format:'YYYY-MM-DD HH:mm:ss',
            applyLabel: '确认',
            cancelLabel: '取消',
            weekLabel: 'W',
            customRangeLabel: 'Custom Range',
            daysOfWeek:["日","一","二","三","四","五","六"],
            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
        }
    }, function(start, end, label) {
        $('#cTime_start').val(start.format('YYYY-MM-DD HH:mm:ss'));
    });
    $("#cTime_start").val("");//清空控件默认时间

    $('#cTime_end').daterangepicker({
        "singleDatePicker": true,
        "timePicker": true,
        "timePicker24Hour": true,
        "timePickerSeconds": true,
        "parentEl": "200",
        locale: {
            format:'YYYY-MM-DD HH:mm:ss',
            applyLabel: '确认',
            cancelLabel: '取消',
            weekLabel: 'W',
            customRangeLabel: 'Custom Range',
            daysOfWeek:["日","一","二","三","四","五","六"],
            monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
        }
    }, function(start, end, label) {
        $('#cTime_end').val(start.format('YYYY-MM-DD HH:mm:ss'));
    });
    $('#cTime_end').val("");//清空控件默认时间

    //日期时间查询条件不为空的情况下，给控件赋值
    if($("#cTimeS_hidden").val() != null || $("#cTimeS_hidden").val() != ""){
        $("#cTime_start").val($("#cTimeS_hidden").val());
    }
    if($("#cTimeE_hidden").val() != null || $("#cTimeE_hidden").val() != ""){
        $("#cTime_end").val($("#cTimeE_hidden").val());
    }


 
  $(".checkbox-toggle").click(function () {
      var clicks = $(this).data('clicks');
      if (clicks) {
        //Uncheck all checkboxes
        $(".mailbox-messages input[type='checkbox']").iCheck("uncheck");
        $(".fa", this).removeClass("fa-check-square-o").addClass('fa-square-o');
      } else {
        //Check all checkboxes
        $(".mailbox-messages input[type='checkbox']").iCheck("check");
        $(".fa", this).removeClass("fa-square-o").addClass('fa-check-square-o');
      }
      $(this).data("clicks", !clicks);
    });

    $(".btn_pay").click(function(){
        var id=$(this).data("pay-id");
        var openid=$("#openid_"+id).val();
        var totalprize=$("#totalprize_"+id).val();
        var salesman_id=$("#salesman_id_" + id).val();

        var payments={cashId:id, totalprize:totalprize, salesman_id:salesman_id, openid:openid};
        bootbox.bconfirm("此次支付共计 </br>￥"+totalprize+"", function (re) {
            if (re) {
                $.putJsonBody("/salesmanCash/pay ", payments, function (re) {
                    window.location.href=window.location.href;
                    window.location.reload;
                })
            }
        })
    });

  $("#batch_pay").click(function () {
	  var payment = new Array();
	  var sumprice = 0;
	  $("input[name='node_checker']").each(function(i){  
	    var isCheck = $(this).prop("checked");
	    var id = $(this).attr("id");
	    if('checked' == isCheck || isCheck){
	    	console.log($("#id"+id).val());
	    	if($("#id"+id).val()=="false"){
	    		$("#"+id).hide();
		    	var json = {};
		    	json.cashId = id;
		    	json.openid = $("#openid_"+id).val();
		    	json.totalprize = $("#totalprize_"+id).val();
		    	json.salesman_id = $("#salesman_id_" + id).val();

 		    	sumprice = Number(sumprice)+Number($("#totalprize_"+id).val());
		    	payment.push(json);
	    	}
	    }
	  });
	  if(payment.length<=0){
		  bootbox.balert("请选择需要支付的记录！");
     	  return false ;
	  }
	  var payments = {List: payment};
	  bootbox.bconfirm("此次支付共计 </br>￥"+sumprice+"", function (re) {
		  if (re) {
			  $("input[name='node_checker']").each(function(i){  
				    var isCheck = $(this).prop("checked");
				    var id = $(this).attr("id");
				    if('checked' == isCheck || isCheck){
				    	//区别是否重复提交
				    	if($("#id"+id).val()=="false"){
				    		$("#id"+id).val("true");
				    	}
				    }
			  });
			  $.putJsonBody("/salesmanCash/batch_pay" , payments, function (re) {
		        	window.location.href=window.location.href;
		        	window.location.reload;
	          })
		  }else{
			  payment = new Array();
			  $("input[name='node_checker']").each(function(i){
				    var isCheck = $(this).prop("checked");
				    var id = $(this).attr("id");
				    if('checked' == isCheck || isCheck){
				    	if($("#id"+id).val()=="false"){
				    		$("#"+id).show();
				    	}
				    }
				  });
		  }
	  });
  });


    //数据导出
    $("[name='btn_export']").click(function () {
        var proName = $("[name='proName']").val();
        var saleman = $("[name='saleman']").val();
        var timeLimitStart = $("[name='timeLimitStart']").val();
        var timeLimitEnd = $("[name='timeLimitEnd']").val();

        //有数据时才能导出
        if($("#resultTotal_hidden").val() != 0){
            bootbox.bconfirm("确定是否导出？", function (re) {
                if (re) {
                    var url="/salesmanCash/export?proName="+proName + "&saleman="+saleman
                        +"&timeLimitStart="+timeLimitStart+"&timeLimitEnd="+timeLimitEnd;
                    window.open(url);
                }
            });
        }else{
            bootbox.balert("没有数据，无法导出!");
        }
    });
  
  
});